package com.ykh.mybbsdemo.config;

import com.ykh.mybbsdemo.handler.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-08-29
 * Time: 20:18
 */

//用来解决跨域问题,因为前后端的端口不一样
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("file:/static/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //跨域配置
        registry.addMapping("/**").allowedOrigins("*");
    }

    //使拦截器生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //假设先拦截test接口, 用于测试, 后续实际遇到需要拦截的接口是=时, 再配置为真正的拦截接口
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/test")
                .addPathPatterns("/comments/create/change")
                .addPathPatterns("/articles/publish")
                .addPathPatterns("/articles/myArticle")
                .addPathPatterns("/articles/update")
                .addPathPatterns("/users/update")
                .addPathPatterns("/focused/**")
                .addPathPatterns("/video/info")
                .addPathPatterns("/video/upload")
                .addPathPatterns("/message/**");
        //拦截所有接口, 除了登录和注册之外
        //registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/login").addPathPatterns("/register");
    }
}


